package com.greateffect.littlebud.lib.utils;

import android.content.SharedPreferences;
import android.os.Build;
import android.text.TextUtils;

import com.greateffect.littlebud.lib.cache.MMKVUtils;
import com.greateffect.littlebud.lib.BuildConfig;
import com.greateffect.littlebud.lib.cache.SharedPreferencesUtil;
import com.greateffect.littlebud.lib.net.CustomNetConfig;
import com.greateffect.littlebud.lib.ui.BaseApplication;
import com.tencent.mmkv.MMKV;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * Created by ZengCS on 2019/1/17.
 * E-mail:zengcs@vip.qq.com
 * Add:成都市天府软件园E3
 */

public class CommonApplication extends BaseApplication {
    @Override
    public void onCreate() {
        super.onCreate();
        // 初始化MMKV
        initMMKV();

        // 初始化HttpManager
        // 读取缓存的服务器类型
        int cacheServerType = MMKVUtils.getInteger(CustomNetConfig.KEY_CURR_ENVIRONMENT, -1);
        if (cacheServerType != -1) {
            CustomNetConfig.updateEnvironment(cacheServerType);
        }
        JLogUtil.d("当前服务器类型为：" + CustomNetConfig.getCurrServerTypeName());
        // 初始化颜色选择器
        JColors.init(getApplicationContext());

        // 解决在Android P上的提醒弹窗 （Detected problems with API compatibility(visit g.co/dev/appcompat for more info)
        if (Build.VERSION.SDK_INT >= 28) {
            closeAndroidPDialog();
        }
    }

    /**
     * 设置MMKV保存文件的路径
     *
     * @return 返回保存的路径
     */
    public String getMMKVRootDir() {
        return null;
    }

    private void initMMKV() {
        JLogUtil.d("MMKV", "初始化MMKV");
        if (TextUtils.isEmpty(getMMKVRootDir())) {
            MMKV.initialize(this);
        } else {
            MMKV.initialize(getMMKVRootDir());
        }
        try {
            // 从SharedPreferences中导入
            MMKV mmkv = MMKV.defaultMMKV();

            Boolean hasImportFromSpV1 = MMKVUtils.getBoolean("HAS_IMPORT_FROM_SP_V1", false);
            JLogUtil.d("MMKV", "hasImportFromSpV1 = " + hasImportFromSpV1);
            if (!hasImportFromSpV1 || BuildConfig.DEBUG) {
                SharedPreferences sp = SharedPreferencesUtil.getSharedPreferences(this);
                if (sp != null) {
                    JLogUtil.d("MMKV", "从SharedPreferences中导入");
                    mmkv.importFromSharedPreferences(sp);
                    mmkv.putBoolean("HAS_IMPORT_FROM_SP_V1", true);
                    JLogUtil.d("MMKV", "导入结束");

                    // 清空SharedPreferences
//                    JLogUtil.d("MMKV", "清空SharedPreferences");
//                    SharedPreferences.Editor editor = sp.edit();
//                    editor.clear();
//                    editor.apply();
//                    JLogUtil.d("MMKV", "清空结束");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void closeAndroidPDialog() {
        try {
            Class aClass = Class.forName("android.content.pm.PackageParser$Package");
            Constructor declaredConstructor = aClass.getDeclaredConstructor(String.class);
            declaredConstructor.setAccessible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            Class cls = Class.forName("android.app.ActivityThread");
            Method declaredMethod = cls.getDeclaredMethod("currentActivityThread");
            declaredMethod.setAccessible(true);
            Object activityThread = declaredMethod.invoke(null);
            Field mHiddenApiWarningShown = cls.getDeclaredField("mHiddenApiWarningShown");
            mHiddenApiWarningShown.setAccessible(true);
            mHiddenApiWarningShown.setBoolean(activityThread, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
